package connect3.team7.util;

import java.net.*;
import java.util.*;

public class IPUtil {

  public static String getHostIP() {
    String ret_ip = null;

    try {
      if (InetAddress.getLocalHost().isLoopbackAddress()) {
        Enumeration e = NetworkInterface.getNetworkInterfaces(); 
  
        while (e.hasMoreElements()) {
          NetworkInterface curr = (NetworkInterface) e.nextElement(); 
  
          Enumeration inetaddrs = curr.getInetAddresses();
  
          while (inetaddrs.hasMoreElements()) {
            InetAddress curr_addr = (InetAddress) inetaddrs.nextElement();
  
            if (!curr_addr.isLoopbackAddress() && (curr_addr instanceof Inet4Address))
              ret_ip = curr_addr.getHostAddress();
          }
        }
      } else {
          ret_ip = InetAddress.getLocalHost().getHostAddress();
      }
    } catch (UnknownHostException ex) {
      ex.printStackTrace();
    } catch (SocketException ex) {
      ex.printStackTrace();
    }

    return ret_ip;
  }
}